#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <mach-o/loader.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef struct mach_header_64 Elf32_Ehdr;

void DumpElf(Elf32_Ehdr *header);

int main(int argc,char **argv)
{
	
	int fd;
	void *ptMapStart;
	
	Elf32_Ehdr *header;
	struct stat fd_stat;
	
	fd = open("/workspace/my-lib/objc-mainspace/ReadElf/a.out",O_RDONLY);
	if(fd ==  0)  
    {
		printf("Open File Error!!!");
		exit(1);
    }
	
	if( fstat(fd, &fd_stat) != 0 ) {
		exit(-1);
	}
	
	ptMapStart = mmap(NULL
					  ,sizeof(Elf32_Ehdr)
					  ,PROT_READ
					  ,MAP_SHARED
					  ,fd
					  ,0);
	
	if(ptMapStart < 0 )  
    {
		printf("map fail!!!!");
		exit(1);
    }
	
	header = (Elf32_Ehdr *)ptMapStart;
	
	//DumpElf(header);
	
	//printf("Num of section headers %d\n", header -> e_shnum);  
	
	munmap(ptMapStart
		   ,sizeof(Elf32_Ehdr));
	
	
	//  printf("%d\n",sizeof(Elf32_Ehdr));
	return 0;
}


